/****************************************************************************
 * Copyright (C) 2012
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * MemCardHandle.hpp
 *
 * for SaveGame Manager GX 2012
 ***************************************************************************/
#ifndef _MEMCARDHANDLE_HPP_
#define _MEMCARDHANDLE_HPP_

#include <vector>
#include <string>

#include "../GC/card.h"
#include "../GC/gci.h"
#include "../GUI/gui_banner.h"

typedef struct _GCStruct
{
	//s32 filenum;
	u8 filename[CARD_FILENAMELEN];
	u8 gamecode_str[4];
	u8 comments[CARD_FILENAMELEN*2];
	u8 permissions;
	u16 blocks;
	
} GCStruct;

class MemCardHandle
{
	public:
		MemCardHandle(int cardSlot);
		~MemCardHandle();
		
		bool Mount();
		void UnMount();
		bool IsInserted();
		bool IsMounted() { return Mounted; }
		
		void GetList();
		int GetListSize() { return EntriesList.size(); }
		GCStruct * GetEntry(int index);
		void ResetList();
		GuiGCIcon * GetIcon(u8* filename, int width = CARD_ICON_W, int height = CARD_ICON_H);
		
	private:
		static void card_removed(s32 chn, s32 result);
		int InternalMount();
		void ReadSectorSize();
		
		int CardSlot;
		bool Mounted;
		unsigned int SectorSize;
		
		void AddEntry();
		int Open(u8* filename, card_file * CardFile);
		bool SetEntry(int cnt, card_dir * CardDir);
		
		GCI gci;
		u8* SysArea;
		std::vector<GCStruct *> EntriesList;
};

#endif
